package com.gun.management.module.task.controller.admin.taskRewardLog;

import com.gun.management.module.task.controller.admin.taskRewardRecord.vo.TaskRewardRecordPageReqVO;
import com.gun.management.module.task.controller.admin.taskRewardRecord.vo.TaskRewardRecordRespVO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.Valid;
import jakarta.servlet.http.HttpServletResponse;
import java.util.*;
import java.io.IOException;

import com.gun.management.framework.common.pojo.PageParam;
import com.gun.management.framework.common.pojo.PageResult;
import com.gun.management.framework.common.pojo.CommonResult;
import com.gun.management.framework.common.util.object.BeanUtils;
import static com.gun.management.framework.common.pojo.CommonResult.success;

import com.gun.management.framework.excel.core.util.ExcelUtils;

import com.gun.management.framework.apilog.core.annotation.ApiAccessLog;
import static com.gun.management.framework.apilog.core.enums.OperateTypeEnum.*;

import com.gun.management.module.task.controller.admin.taskRewardLog.vo.*;
import com.gun.management.module.task.dal.dataobject.taskRewardLog.TaskRewardLogDO;
import com.gun.management.module.task.service.taskRewardLog.TaskRewardLogService;

@Tag(name = "管理后台 - 任务奖励日志信息")
@RestController
@RequestMapping("/task/reward-log")
@Validated
public class TaskRewardLogController {

    @Resource
    private TaskRewardLogService rewardLogService;

    @PostMapping("/create")
    @Operation(summary = "创建任务奖励日志信息")
    @PreAuthorize("@ss.hasPermission('task:reward-log:create')")
    public CommonResult<Long> createRewardLog(@Valid @RequestBody TaskRewardLogSaveReqVO createReqVO) {
        return success(rewardLogService.createRewardLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新任务奖励日志信息")
    @PreAuthorize("@ss.hasPermission('task:reward-log:update')")
    public CommonResult<Boolean> updateRewardLog(@Valid @RequestBody TaskRewardLogSaveReqVO updateReqVO) {
        rewardLogService.updateRewardLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除任务奖励日志信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('task:reward-log:delete')")
    public CommonResult<Boolean> deleteRewardLog(@RequestParam("id") Long id) {
        rewardLogService.deleteRewardLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得任务奖励日志信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('task:reward-log:query')")
    public CommonResult<TaskRewardLogRespVO> getRewardLog(@RequestParam("id") Long id) {
        TaskRewardLogDO rewardLog = rewardLogService.getRewardLog(id);
        return success(BeanUtils.toBean(rewardLog, TaskRewardLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得任务奖励记录信息分页")
    @PreAuthorize("@ss.hasPermission('task:reward-log:page')")
    public CommonResult<PageResult<TaskRewardLogRespVO>> getRewardLogPage(@Valid TaskRewardLogPageReqVO pageReqVO) {
        PageResult<TaskRewardLogRespVO> pageResult = rewardLogService.getRewardLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TaskRewardLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出任务奖励记录信息 Excel")
    @PreAuthorize("@ss.hasPermission('task:reward-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRewardLogExcel(@Valid TaskRewardLogPageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TaskRewardLogRespVO> list = rewardLogService.getRewardLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "任务奖励记录信息.xls", "数据", TaskRewardLogRespVO.class,
                BeanUtils.toBean(list, TaskRewardLogRespVO.class));
    }


}